#!/usr/bin/env python3.2
"""WikiBase using PyDbLite with a SQLite backend
"""
import wikibase_pydblite
from wikibase_pydblite import Page
from PyDbLite import SQLite
#~ from datetime import datetime

def _log(self, *args):
    import sys
    for arg in args:
        sys.stderr.write('%s\n' % arg)

class WikiBase(wikibase_pydblite.WikiBase):
    def __init__(self, path):
        db = SQLite.Table('wiki',path)
        db.create(('name','TEXT'),
            ('content','TEXT'),
            ('admin','INTEGER'),
            ('nbvisits','INTEGER'),
            ('created','BLOB'),
            ('version','INTEGER'),
            ('lastmodif','BLOB'),
            mode="open")
        db.is_datetime('created')
        db.is_datetime('lastmodif')
        self.db = db
    
def wikibase(path):
    return WikiBase(path)

if __name__ == "__main__":   
    db = wikibase('test.sqlite')
    page = db.get('NewPage')
    print(bool(page))
    if page:
        # Existing page
        page.content += '\nExtra stuff\n'
    else:
        # a new page
        page.content = 'Some new text\n'
    db.put(page)
    page = db.get('YetAnotherPage')
    if not page:
        print('page is new')
    page.content += 'Text for yet another page\n'
    db.put(page)
    print(db.get('NewPage'))
    print(db.get('YetAnotherPage'))
    print()
    for page in db.iter_pages(limit=3):
        print(page)
    print([r.as_dict() for r in db.iter_pages()])
